<template>
	<view class="content padding">
		<u-navbar safeAreaInsetTop placeholder fixed autoBack>
			<view class="u-nav-slot color_ling text_weight" slot="center">运费模板</view>
			<view class="u-nav-slot text_a color_san" slot="right" @click="goAddFreightTemplage('')">
				<image class="imgBox" src="../static/image/shop/jia.png" mode=""></image>
			</view>
		</u-navbar>
		<!-- 列表 -->
		<view class="padding border_2 ffffff margin_bottom3" style="position: relative;" v-for="(item,index) in list.data"
			:key="item.id" @click="checkYunfei(item)">
			<view class="color_ling text_w padding_bottom border_bottom">{{item.name}}</view>
			<view class="typeBox">
				<!-- // 计价方式:1=按件数,2=按重量,3=按体积 -->
				<view class="number" v-if="item.is_delivery==0&&item.valuation==1">件数</view>
				<view class="weight" v-if="item.is_delivery==0&&item.valuation==2">重量</view>
				<view class="tiji" v-if="item.is_delivery==0&&item.valuation==3">体积</view>
				<view class="baoyou" v-if="item.is_delivery==1">包邮</view>
			</view>
			<view class="margin_top">
				<view class="flexright">
					<view class="flexleft margin_right6" @click.stop="goAddFreightTemplage(item.id)">
						<image class="imgEdit margin_right1" src="/static/image/mine/edit.png" mode=""></image>
						<view class="color_ling text_a">编辑</view>
					</view>
					<view class="flexleft" @click.stop="del(item.id)">
						<image class="imgDelete margin_right1" src="/static/image/mine/delete.png" mode=""></image>
						<view class="color_ling text_a">删除</view>
					</view>
				</view>
			</view>
		</view>
		<view class="flexcenter placeholderBoxTop" v-if="!list.data.length">
			<image class="placeholderBox" src="/static/placeholderimg.png" mode=""></image>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				page: 1,
				list: {
					data: []
				},
				type:0,
			}
		},
		onLoad(options) {
			if(options.type){
				this.type = options.type
			}
		},
		onShow() {
			this.init();
		},
		methods: {
			checkYunfei(item){
				if(this.type==1){
					let data = {
						id:item.id,
						name:item.name
					}
					uni.$emit("selectYunfei",data)
					uni.navigateBack(1)
				}
			},
			init(isPage, page) {
				let _this = this;
				httpRequest.request('/api/shop.logistics/getlist', 'GET', {
					page: page || 1,
				}, false, false, true).then(res => {
					if (res.code == 1) {
						let resList = res.data;
						let dataList = _this.list;
						if (isPage == true) {
							_this.list.data = dataList.data.concat(resList.data);
						} else {
							this.list = res.data
							_this.list.data = resList.data;
						}
					}

				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			onReachBottom() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.init(true, page);
			},
			del(id) {
				httpRequest.modal('提示', '请再次确认', true, (res) => {
					if (res) {
						httpRequest.request('/api/shop.logistics/del', 'POST', {
							id
						}).then(res => {
							httpRequest.toast(res.msg)
							if (res.code == 1) {
								this.init()
							}
						})
					}
				})
			},
			//设为默认
			edit(item) {
				if (!item.is_default) {

					httpRequest.request('/api/address/setdefault', 'POST', {
						id: item.id
					}).then(res => {
						httpRequest.toast(res.msg)
						if (res.code == 1) {
							this.init()
						}
					})
				}
			},
			goAddFreightTemplage(id) { //添加运费模板
				uni.navigateTo({
					url: "./addFreightTemplate?id=" + id
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.imgBox {
		width: 38rpx;
		height: 38rpx;
	}

	.border_bottom {
		border-bottom: 1rpx solid rgba(153, 153, 153, .1);
	}

	.typeBox {
		position: absolute;
		top: 0;
		right: 0;

		.number {
			width: 107rpx;
			height: 42rpx;
			line-height: 42rpx;
			text-align: center;
			background: rgba(0, 188, 19, 0.1);
			border-radius: 0rpx 14rpx 0rpx 14rpx;
			font-size: 24rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #00BC13;
		}

		.tiji {
			width: 107rpx;
			height: 42rpx;
			line-height: 42rpx;
			text-align: center;
			background: rgba(249, 217, 118, 0.1);
			border-radius: 0rpx 14rpx 0rpx 14rpx;
			font-size: 24rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #f5d370;
		}

		.baoyou {
			width: 107rpx;
			height: 42rpx;
			line-height: 42rpx;
			text-align: center;
			background: rgba(78, 110, 242, 0.1);
			border-radius: 0rpx 14rpx 0rpx 14rpx;
			font-size: 24rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color:#4e6ef2;
		}

		.weight {
			width: 107rpx;
			height: 42rpx;
			line-height: 42rpx;
			text-align: center;
			background: rgba(243, 39, 28, 0.1);
			border-radius: 0rpx 14rpx 0rpx 14rpx;
			font-size: 24rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #F3271C;
		}
	}

	.imgEdit {
		width: 18.67rpx;
		height: 24.27rpx;
		display: block;
	}

	.imgDelete {
		width: 23.33rpx;
		height: 24.27rpx;
		display: block;
	}
</style>